package com.zm.demo.core.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 计数质数
 * @author zoum
 * @create 2020/1/10 16:30
 */
public class CountPrimes {

    /**
     * 艾拉托斯特尼筛选法
     * @param n
     * @return
     */
    public int countPrimes(int n) {
        int count=0;
        boolean[] noP = new boolean[n+1];
//        List<Boolean> noP = new ArrayList<>();
        for (int i = 2; i < n; i++) {
            if (noP[i] == false){
                count++;
                for (int j = i; i*j < n; j++) {
                    noP[i*j]=true;
                }
            }
        }
        return count;
    }
}
